#include "stdafx.h"
#include "fluid_model.h"
#include "units.h"

FluidModel::FluidModel(Common* _common, const QString &_name, const int &_type)
	: common(_common), name(_name), type((type_t)_type)
{
	init_model();
}


FluidModel::~FluidModel(){}

void FluidModel::init_model(){
	//0 - combobox/boolean
	//1 - spinbox
	//2 - textbox
	//3 - label

	QString common_parameter = "COMMON";

	model = new ParametersModel(this);
	model->addItem(common_parameter, "NAME", name, "", 2, 1, "test1");
	if (isOil()){
		model->addItem(common_parameter, "DENSITY", 0, "kg/m3", 1, 1, "test2");
		model->addItem(common_parameter, "VISCOSITY//DINAMIC", 0, "CP", 1, 1, "test4");
		model->addItem(common_parameter, "VISCOSITY//KINEMATIC", 0, "-", 1, 0, "test5");
		model->addItem(common_parameter, "SVPRESSURE", 0, "Pa", 1, 0, "saturated vapour pressure");
		model->addItem(common_parameter, "SOUND SPEED", 0, "km/h", 1, 1, "SOUND SPEED");
	}else if(isGas()){
		model->addItem(common_parameter, "GAS_PARAM1", 0, "kg/m3", 1, 1, "test2");
		//................
	}else if(isComposition()){
		model->addItem(common_parameter, "CH4", 0, "-", 1, 1, "metan");
		model->addItem(common_parameter, "C2H6", 0, "-", 1, 1, "etan");
		model->addItem(common_parameter, "C3H8", 0, "-", 1, 1, "C3H8");
		model->addItem(common_parameter, "iC4H10", 0, "-", 1, 1, "i-butan");
		model->addItem(common_parameter, "C4H10", 0, "-", 1, 1, "butan");
		model->addItem(common_parameter, "iC5H12", 0, "-", 1, 1, "i-pentan");
		model->addItem(common_parameter, "C5H12", 0, "-", 1, 1, "pentan");
		model->addItem(common_parameter, "iC6H14", 0, "-", 1, 1, "i-geksan");
		model->addItem(common_parameter, "C6H14", 0, "-", 1, 1, "geksan");
		model->addItem(common_parameter, "C7H16", 0, "-", 1, 1, "C7H16");
		model->addItem(common_parameter, "N2", 0, "-", 1, 1, "N2");
		model->addItem(common_parameter, "CO2", 0, "-", 1, 1, "CO2");

		model->addItem(common_parameter, "TOTAL", 0, "-", 3, 1, "total");
		//................
	}

	connect(this, SIGNAL(setNewParameterValue(const QString &, const QVariant &)), 
		model, SLOT(changeData(const QString &, const QVariant &)));

	connect(model, SIGNAL(ItemChanged(const QString &, const QVariant &)),
		this, SLOT(getNewParameterValue(const QString &, const QVariant &)));


}

void FluidModel::update_parameters(){
	common->getParametersWidget()->setItemDelegate(new Delegate(common->getParametersWidget()));
}

void FluidModel::getNewParameterValue( const QString &_name, const QVariant &_value ){
	if ( _name == "TOTAL" ) return;
	if(isComposition()){
		QStringList component_list; 
		component_list <<  "CH4" << "C2H6" << "C3H8" << "iC4H10" << "C4H10" << "iC5H12" << "C5H12" << "iC6H14" << "C6H14" << "C7H16" << "N2" << "CO2";
		double total = 0;
		foreach(QString component, component_list){
			double t = model->getNumParameter(component);
			total += t;
		}
		double coeff = g_units.getCoeff(model->getUnit("TOTAL"));
		setNewParameterValue("TOTAL", coeff * total);
	}
}

//========================================================================================================================================

static const QColor indexUsedColor = QColor(167, 211, 92);

FluidModelEitor::FluidModelEitor(Common* _common) : common(_common)
	//: QDialog(pwgt, Qt::WindowTitleHint | Qt::WindowSystemMenuHint)
{
	ui.setupUi(this);
	connect(ui.bAdd, SIGNAL(clicked), this, SLOT(on_bAdd_clicked));
	connect(ui.bRemove, SIGNAL(clicked), this, SLOT(on_bRemove_clicked));
	connect(ui.listOfModels, SIGNAL(currentRowChanged(int)), this, SLOT(displayFluidModel(int)));
	connect(ui.bUse, SIGNAL(clicked), this, SLOT(on_bUse_clicked));
}

FluidModelEitor::~FluidModelEitor(){
}

void FluidModelEitor::on_bAdd_clicked(){
	FluidModelAddForm fluid_model_add_form(common);
	if (fluid_model_add_form.exec()){
		pair<QString, int> data = fluid_model_add_form.getData();
		QString name = data.first;
		int type = data.second;
		if (type == -1) return;
			if(name.isEmpty()){
				QMessageBox::critical(0, tr("Error"), tr("Empty name is not valid!"), QMessageBox::Ok);	
				return;
			}
			for (int i = 0; i < ui.listOfModels->count(); i++)
				if(ui.listOfModels->item(i)->text() == name){
					QMessageBox::critical(0, tr("Error"), tr("That name already exists!"), QMessageBox::Ok);	
					return;
				}
				FluidModelList.push_back(new FluidModel(common, name, type));
				QString icon_path = QString("img/fluid/%1.png").arg(type == 0 ? "oil" : (type == 1 ? "gas" : "composition"));
				ui.listOfModels->addItem(new QListWidgetItem(QIcon(icon_path), name));
				ui.listOfModels->setCurrentRow(ui.listOfModels->count() - 1);
				displayFluidModel(ui.listOfModels->currentRow());	
				//common->log("Oil model: '" + name + "' has been added");
	}
}

void FluidModelEitor::on_bRemove_clicked(){
	if (ui.listOfModels->currentRow() == -1){
		QMessageBox::information(0, tr("Removing oil model..."), tr("Model is not selected!"), QMessageBox::Ok);	
		return;
	}
	if(QMessageBox::question(0, tr("Removing oil model..."), QString(tr("Remove the oil model: '%1'?")).arg(ui.listOfModels->currentItem()->text()),
		QMessageBox::Yes |QMessageBox::No) == QMessageBox::Yes)
	{	
		FluidModelList.erase(FluidModelList.begin() + ui.listOfModels->currentRow());
		//QString name = ui.listOfModels->currentItem()->text();
		delete ui.listOfModels->takeItem(ui.listOfModels->currentRow());
		displayFluidModel(ui.listOfModels->currentRow());
		//common->log("Oil model: '" + name + "' has been removed");
	}
}

void FluidModelEitor::on_bUse_clicked(){
	for(int i = 0; i < ui.listOfModels->count(); i++){
		if (i == ui.listOfModels->currentRow()){
			ui.listOfModels->item(i)->setBackgroundColor(indexUsedColor);
		}else{
			ui.listOfModels->item(i)->setBackgroundColor(Qt::white);
		}
	}
}

void FluidModelEitor::displayFluidModel(int index){
	if (index != -1) setModel(FluidModelList[index]->model);
	else setModel(new ParametersModel());
	for(int i = 3; i <= 8; i++)
		ui.trParameters->hideColumn(i);
}

void FluidModelEitor::setModel(ParametersModel* _model){
	ui.trParameters->setModel(_model);
	ui.trParameters->setItemDelegate(new Delegate(common->getParametersWidget()));
	ui.trParameters->setStyleSheet("alternate-background-color: rgb(190, 234, 235)");
	for(int i = 3; i <= 8; i++)
		ui.trParameters->hideColumn(i);
	ui.trParameters->header()->setStretchLastSection(false);
	ui.trParameters->header()->setSectionResizeMode(1, QHeaderView::Stretch);   
	ui.trParameters->setColumnWidth(0, 120);
	ui.trParameters->setColumnWidth(2, 40);
}

void FluidModelEitor::setData(vector<FluidModel*> _FluidModelList, const QString _current_fluid){
	FluidModelList = _FluidModelList;
	for (int i = 0; i < FluidModelList.size(); i++){
		int type = FluidModelList[i]->getIntType();
		QString icon_path = QString("img/fluid/%1.png").arg(type == 0 ? "oil" : (type == 1 ? "gas" : "composition"));
		ui.listOfModels->addItem(new QListWidgetItem(QIcon(icon_path), FluidModelList[i]->getName()));
		if (FluidModelList[i]->getName() == _current_fluid) 
			ui.listOfModels->item(i)->setBackgroundColor(QColor(167, 211, 92));
	}
}

vector<FluidModel*> FluidModelEitor::getData(){
	return FluidModelList;
}

QString FluidModelEitor::getCurrentFluidModelName(){
	int index_of_model_used;
	for(int i = 0; i < ui.listOfModels->count(); ++i)
		if (ui.listOfModels->item(i)->backgroundColor() == indexUsedColor)
			return ui.listOfModels->item(i)->text();
	return "";
}
//========================================================================================
FluidModelAddForm::FluidModelAddForm(QWidget* pwgt){
	ui.setupUi(this);
	ui.txtFluidName->setText("fluid");
}	

FluidModelAddForm::~FluidModelAddForm(){}

pair<QString, int> FluidModelAddForm::getData(){
	QString name = ui.txtFluidName->text();
	int i = -1;
		if (ui.rbOil->isChecked())
		i = 0;
	else if (ui.rbGas->isChecked())
		i = 1;
	else if (ui.rbComposition->isChecked())
		i = 2;
	return make_pair(name, i);	
}